/*
student_index.jsp页面
*/
var currentpage=1;
var totalpage=0;
var currentrecord=1;
/**
 * 每页显示条数
 */
var range=10;
var totalcount=0;
/**
 * pp即perpage，正常与range语义一致，
 * 主要为解决数据库记录数小于range而作为range的拷贝
 */
var pp=range;



/**
 * 页面载入自动触发，作获取记录数操作，在异步回调函数中再获取内容
 * 主要是避免range小于数据库记录数而先获取记录数再请求内容
 */ 
$(document).ready(function () {
	//alert("gggg");
	$('#spfirst').click(firstfun);
	$('#sppre').click(prefun);
	$('#spnext').click(nextfun);
	$('#splast').click(lastfun);
	//$('#queryPlanBtn').click(findPlanNoReportRC);
	$('#otherPlan').toggle(
		function(){
			$('#hideMorePlan').show();
			$('#otherPlanFrame').attr("height", $('body', window.frames['otherPlanFrame'].document).height());
			//$('#otherPlanFrame', window.parent.document).attr("height", $("body").height());
		},
		function(){
			$('#hideMorePlan').hide();
			$('#otherPlanFrame').attr("height", $('body', window.frames['otherPlanFrame'].document).height());
		}
	);
	findPlanNoReportRC();
	/**
	 * 启动预案
	 */
	$('#checkPlanBtn').click(function () {
		//delPlanById($(this));
		//alert("dfgdfg");
		//window.parent.location.href="/ag/zhh/index.jsp";
		//setEventPlan2Session();
		var slt = "";
		if($("[name='pdRedio']:checked",window.frames['otherPlanFrame'].document).val()!=undefined){
			slt=$("[name='pdRedio']:checked",window.frames['otherPlanFrame'].document).val();
		}else{
			slt=$("[name='pdRedio']:checked",window.frames['matchFrame'].document).val();
		}
		if(slt==undefined){
			alert("请选择预案！");
			return;
		}else{
			//alert(slt);
			startPlan(slt);
		}
		//startPlan(slt);
	});
});


function startPlan(PId){
	var EId = getCookie(eventId);
    var url = 'work2/LZMReport_startPlan.action';
    var params = {
        'EId': EId,
		'PId': PId
    };
    jQuery.post(url, params, startPlanRst, 'json');
}

function startPlanRst(data){
	/**
	 * 如果成功的话，则转到瑞童的带预案步骤的界面
	 */
	//alert("setEventPlan2SessionRst");
	if(data.ajaxRst=="success"){
		//window.parent.parent.location.href="/ag/zrt/loadPlanStep.action";
		//setEventPlan2Session();
//		window.location.href="/ag/work/zhzrm.jsp";		//这个用来选择指挥长的页面，不要了
		var planLevel=$.trim($("#planLevel").text());	//得到等级，以1,2,3这样的形式存在
//		alert(planLevel+"***");	
//		var url_updateEventLevel="/ag/dgm/updateEventLevel_DGMdisposalAdvice.action";
//		$.post(url_updateEventLevel,{'eventLevel':planLevel},function(data){					//更新事件等级的	,于11_9号dgm注,更新事件等级不应该在这里更新,应该在选择几级预案更新		
//				});
		window.location.href="/ag/work/reportTable.jsp";
	}else{
		//alert("事件已经启动相同预案，请选择其他预案！");
		alert("新增预案失败，请重新增加");
	}
}


/**
 * 请求颢铧Sesssion
 */
function setEventPlan2Session(){	
	//alert("dsffds");
	var timestamp = Date.parse(new Date());
	$.get("/ag/do/processStart_processEventStartByPlan.action?ts="+timestamp+"&eventId="+getCookie(eventId),null,setEventPlan2SessionRst);
}

function setEventPlan2SessionRst(data){
	/**
	 * 如果成功的话，则转到瑞童的带预案步骤的界面
	 */
	//alert("setEventPlan2SessionRst");
	if(data.result=="success"){
		window.parent.parent.location.href="/ag/zrt/loadPlanStep.action";
	}else{
		alert("服务器异常，请检查");
	}
}


function findMatchPlan(){
	//alert($('#eventIDSP').text());
	var timestamp = Date.parse(new Date());
	$.get("lzm/PlanStandard_findMatchPlan.action?ts="+timestamp+"&eventID="+$('#eventIDSP').text(),null,findMatchPlanRst);
}

function findMatchPlanRst(data){
	//alert("pid");
	//alert(data.planList[0].PId);
	var timestamp = Date.parse(new Date());
	$.get("lzm/Plan_findPlanById.action?ts="+timestamp+"&PId="+data.planList[0].PId,null,fillDatatoHTML1);
}

function fillDatatoHTML1(data){	
	var plan=data.tbPlan;
	var strhtml;	
	strhtml="<tr><td height='18' bgcolor='#FFFFFF'><div align='center' class='STYLE1'>";
	strhtml+="<input name='checkbox' type='button' class='STYLE2 planDetail myBtn' value='查看' /><span style='display:none'>";
	strhtml+=plan.PId;
	strhtml+="</span>";
	strhtml+="</div></td><td height='18' bgcolor='#FFFFFF' class='STYLE2'>";
	
	strhtml+="<div align='center' class='STYLE2 STYLE1'><input name='radio' type='radio' id='radio' value=";
	strhtml+=plan.PId;
	strhtml+=" checked='checked' />";
	strhtml+="</div></td><td height='18' bgcolor='#FFFFFF' class='STYLE2'>";
	
	strhtml+="<div align='center' class='STYLE2 STYLE1'>";
	strhtml+=plan.PId;
	strhtml+="</div></td><td height='18' bgcolor='#FFFFFF'><div align='center' class='STYLE2 STYLE1'>";
	strhtml+=plan.tbPlanType.ptName;
	strhtml+="</div></td><td height='18' bgcolor='#FFFFFF'><div align='center' class='STYLE2 STYLE1'>";
	strhtml+=plan.tbPlanGrade.pgName;
	strhtml+="</div></td><td height='18' bgcolor='#FFFFFF'><div align='center' class='STYLE2 STYLE1'>";
	strhtml+="已审核";
	strhtml+="</div></td><td height='18' bgcolor='#FFFFFF'><div align='center' class='STYLE2 STYLE1'>";
	strhtml+=plan.tbPlanRegion.prName;
	strhtml+="</div></td></tr>";
	/*
	strhtml+="</div></td><td height='18' bgcolor='#FFFFFF'><div align='center'>";
	strhtml+="<img src='images/037.gif' width='9' height='9' /><span class='STYLE1'>";
	strhtml+=" [</span><span style='display:none'>";
	strhtml+=tbPlanPId;
	strhtml+="</span><a href='javascript:;' class='editPlan'>编辑</a><span style='display:none'>";
	strhtml+=tbPlanPId;
	strhtml+="</span><span class='STYLE1'>]</span></div></td>";
	strhtml+="<td height='18' bgcolor='#FFFFFF'><div align='center'><span class='STYLE2'>";
	strhtml+="<img src='images/010.gif' width='9' height='9' /> </span><span class='STYLE1'>";
	strhtml+="[</span><span style='display:none'>";
	strhtml+=tbPlanPId;
	strhtml+="</span><a href='javascript:;' class='delPlan'>删除</a><span style='display:none'>";
	strhtml+=tbPlanPId;
	strhtml+="</span><span class='STYLE1'>]</span></div></td></tr>";	
	*/
	$('#loadingTR').hide();
	//alert("dfdfgd");
	$('#studentTable1').append(strhtml);	
	$('.planDetail').click(function () {
		setCookie(PId,$(this).next().text());
		window.location.href="start_plan_rle.jsp";
	});
	$('.editPlan').click(function () {
		setCookie(PId,$(this).next().text());
		window.location.href="edit_plan_rle.jsp";
	});
	$('.delPlan').click(function () {
		delPlanById($(this));
	});		
}


/**
 * 作获取所有预案所有记录数操作
 */
function findPlanNoReportRC(){
	$('#noneTR').hide();
	$('#planTable').show();
	$('#studentTable').children().remove();
	$('#loadingTR').show();
	var url = 'lzm/Plan_findAllPlanRC.action';
	var params = {
			"queryOption['planType']":0,
			"queryOption['planStatus']":0,
			"queryOption['planScope']":0,
			"queryOption['planRegion']":0,
			"queryOption['planGrade']":0,
			planStatus:RELEASE
	}; 
	jQuery.post(url, params, findPlanNoReportRCRst, 'json');
}

/**
 * 作获取预案所有记录数回调函数
 */
function findPlanNoReportRCRst(data){
	if(data.recordCount==0){
		$('#loadingTR').hide();
		$('#noneTR').show();		
		$('#currpage').text(0);
		$('#countpp').text(0);
		$('#totalpage').text(0);	
		$('#sprecvct').text(0);	
	}else{
		$('#noneTR').hide();
		totalcount=data.recordCount;
		totalpage=Math.ceil(totalcount/range);
		$('#sprecvct').text(totalcount);
		if(totalcount<range){
			pp=totalcount;
		}
		findPartByPage(1,pp);
	}
}


/**
 * 发送异步请求获取分页数据
 */
function findPartByPage(first,max){
	$('#studentTable').children().remove();
	$('#loadingTR').show();
	var url = 'lzm/Plan_findAllPlan.action';
	var params = {
			firstRow : first-1,
			maxRow : max,
			"queryOption['planType']":0,
			"queryOption['planStatus']":0,
			"queryOption['planScope']":0,
			"queryOption['planRegion']":0,
			"queryOption['planGrade']":0,
			planStatus:RELEASE
	}; 
	jQuery.post(url, params, findPartByPageRst, 'json');
}


function delPlanById(myself){	
	var timestamp = Date.parse(new Date());
	$.get("lzm/Plan_delPlanById.action?ts="+timestamp+"&PId="+myself.next().text(),null,delPlanByIdRst);
}

function delPlanByIdRst(data){
	if(data.ajaxRst=SUCCESS){
		alert("删除数据成功");
		findPlanNoReportRC();
	}else{
		alert("删除数据失败");
	}
}


/**
 * 发送异步请求获取分页数据回调函数
 */
function findPartByPageRst(data){
	$.each(data.planList,function(i,n){
		fillDatatoHTML(n.PId,n.PName,n.tbPlanGrade.pgName,n.tbPlanStatus.pstName,n.tbPlanType.ptName);
	});
	$('.planDetail').click(function () {
		setCookie(PId,$(this).next().text());
		window.location.href="start_plan_rle.jsp";
	});
	$('.editPlan').click(function () {
		setCookie(PId,$(this).next().text());
		window.location.href="edit_plan_rle.jsp";
	});
	$('.delPlan').click(function () {
		delPlanById($(this));
	});
	calculatepage();
	/**
	 * 获取当前事件匹配预案
	 */
	findMatchPlan();
}

/**
 * DOM操作，动态生成HTML来显示内容
 */
function fillDatatoHTML(tbPlanPId,ptName,pgName,pstName,prName){	
	var strhtml;	
	strhtml="<tr><td height='18' bgcolor='#FFFFFF'><div align='center' class='STYLE1'>";
	strhtml+="<input name='checkbox' type='button' class='STYLE2 planDetail myBtn' value='查看' /><span style='display:none'>";
	strhtml+=tbPlanPId;
	strhtml+="</span>";
	strhtml+="</div></td><td height='18' bgcolor='#FFFFFF' class='STYLE2'>";
	
	strhtml+="<div align='center' class='STYLE2 STYLE1'><input name='radio' type='radio' value=";
	strhtml+=tbPlanPId;
	strhtml+=" />";
	strhtml+="</div></td><td height='18' bgcolor='#FFFFFF' class='STYLE2'>";
	
	strhtml+="<div align='center' class='STYLE2 STYLE1'>";
	strhtml+=tbPlanPId;
	strhtml+="</div></td><td height='18' bgcolor='#FFFFFF'><div align='center' class='STYLE2 STYLE1'>";
	strhtml+=ptName;
	strhtml+="</div></td><td height='18' bgcolor='#FFFFFF'><div align='center' class='STYLE2 STYLE1'>";
	strhtml+=pgName;
	strhtml+="</div></td><td height='18' bgcolor='#FFFFFF'><div align='center' class='STYLE2 STYLE1'>";
	strhtml+="已审核";
	strhtml+="</div></td><td height='18' bgcolor='#FFFFFF'><div align='center' class='STYLE2 STYLE1'>";
	strhtml+=prName;
	strhtml+="</div></td></tr>";
	/*
	strhtml+="</div></td><td height='18' bgcolor='#FFFFFF'><div align='center'>";
	strhtml+="<img src='images/037.gif' width='9' height='9' /><span class='STYLE1'>";
	strhtml+=" [</span><span style='display:none'>";
	strhtml+=tbPlanPId;
	strhtml+="</span><a href='javascript:;' class='editPlan'>编辑</a><span style='display:none'>";
	strhtml+=tbPlanPId;
	strhtml+="</span><span class='STYLE1'>]</span></div></td>";
	strhtml+="<td height='18' bgcolor='#FFFFFF'><div align='center'><span class='STYLE2'>";
	strhtml+="<img src='images/010.gif' width='9' height='9' /> </span><span class='STYLE1'>";
	strhtml+="[</span><span style='display:none'>";
	strhtml+=tbPlanPId;
	strhtml+="</span><a href='javascript:;' class='delPlan'>删除</a><span style='display:none'>";
	strhtml+=tbPlanPId;
	strhtml+="</span><span class='STYLE1'>]</span></div></td></tr>";	
	*/
	$('#loadingTR').hide();
	$('#studentTable').append(strhtml);			
}

/**
 * 计算页数，实际上是显示页数的变化
 * 当前是第1页   共1页   首页 上一页 下一页 末页   本页显示23条记录 
 */
function calculatepage(){
	$('#currpage').text(currentpage);
	$('#countpp').text(pp);
	$('#totalpage').text(totalpage);	
}

/**
 * 首页处理函数
 */
function firstfun(){
	if(currentpage!=1){
		findPartByPage(1,range);
		currentpage=1;
		currentrecord=1;
		pp=range;
	}
}

/**
 * 上一页处理函数
 */
function prefun(){
	if(currentpage!=1){
		currentrecord-=range;		
		findPartByPage(currentrecord,range);
		currentpage--;
		pp=range;
	}
}

/**
 * 下一页处理函数
 */
function nextfun(){
	if(currentpage!=totalpage){	
		var t=Math.ceil(totalcount%range);
		currentrecord+=range;	
		if(currentpage!=totalpage-1){			
			findPartByPage(currentrecord,range);
			pp=range;
		}
		else{
			if(t==0) t=range;
			findPartByPage(currentrecord,t);
			pp=t;
		}		
		currentpage++;		
	}
}

/**
 * 末页处理函数
 */
function lastfun(){
	if(currentpage!=totalpage){
		var t=Math.ceil(totalcount%range);
		if(t==0) t=range;
		currentrecord=totalcount-t+1;
		findPartByPage(currentrecord,t);
		currentpage=totalpage;		
		pp=t;
	}
}
function displayPlanTree(){
	window.location.href="find_plan_container.jsp";
}
/*
function delStudentById(myself){	
	var timestamp = Date.parse(new Date());
	$.get("../teacher/teacher_delStudentById.action?ts="+timestamp+"&SNo="+myself.prev().text(),null,delStudentByIdRst);
}

function delStudentByIdRst(data){
	if(data.ajaxRst==SUCCESS){
		operateSucc("teacher/student_index.jsp");
	}else{
		alert("删除学生失败");
	}
}*/